//
//  E7Country.cpp
//  EndlessBattle
//
//  Created by cc on 12-3-17.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7Country.h"

#include "E7Logic.h"

CE7Country::CE7Country()
{
}

CE7Country::~CE7Country()
{
    m_listTownIDs.clear() ;
    m_listCellIDs.clear() ;
}

bool CE7Country::InitData(int nID)
{
    m_listTownIDs.clear() ;
    m_listCellIDs.clear() ;

    m_nID = nID ;

    return true ;
}

bool CE7Country::AddTown(int nID)
{
//    printf("enter country add town\n") ;
    TOWN_ID_LIST::iterator itr ;
    for (itr = m_listTownIDs.begin(); itr != m_listTownIDs.end(); ++itr)
    {
        if ((*itr) == nID)
        {
            printf("country add town error : [%d] was added\n", nID) ;
            return false ;
        }
    }

    m_listTownIDs.push_back(nID) ;

    return true ;
}

bool CE7Country::DelTown(int nID)
{
    TOWN_ID_LIST::iterator itr ;
    for (itr = m_listTownIDs.begin(); itr != m_listTownIDs.end(); ++itr)
    {
        if ((*itr) == nID)
        {
            m_listTownIDs.erase(itr) ;
//            s_pLogic->DelSelectTownInfo(nID) ;
//            printf("country delete town : [%d]\n", nID) ;
            return true ;
        }
    }

    printf("country delete town error : [%d] is not found\n", nID) ;
    return false ;
}

bool CE7Country::AddCell(int nID)
{
    CELL_ID_LIST::iterator itr ;
    for (itr = m_listCellIDs.begin(); itr != m_listCellIDs.end(); ++itr)
    {
        if ((*itr) == nID)
        {
            printf("country add cell error : [%d] was added\n", nID) ;
            return false ;
        }
    }

    m_listCellIDs.push_back(nID) ;
    
    return true ;
}

bool CE7Country::DelCell(int nID)
{
    CELL_ID_LIST::iterator itr ;
    for (itr = m_listCellIDs.begin(); itr != m_listCellIDs.end(); ++itr)
    {
        if ((*itr) == nID)
        {
            m_listCellIDs.erase(itr) ;
            return true ;
        }
    }

    printf("country delete cell error : [%d] is not found\n", nID) ;
    return false ;
}

bool CE7Country::CreateTown()
{
    CELL_ID_LIST::iterator itr_cellid ;
    CELL_INFO_LIST::iterator itr_cellinfo ;
    CE7Cell* pCell = NULL ;
    bool flag = false ;
    for (itr_cellid = m_listCellIDs.begin(); itr_cellid != m_listCellIDs.end(); ++itr_cellid)
    {
        pCell = s_pLogic->ActiveMap()->GetSelectCellInfo((*itr_cellid)) ;
        if (-1 == pCell->GetTownID())
        {
            flag = false ;
            for (int i = 0; i < 6; ++i)
            {
                if (NULL != pCell->GetNeighborPointer(i) &&
                    pCell->GetCountryID() == pCell->GetNeighborPointer(i)->GetCountryID())
                {
                    if (pCell->GetNeighborPointer(i)->GetTownID() != -1)
                    {
                        printf("CE7Country::CreateTown error\n") ;
                        return false ;
                    }
                    else
                    {
                        // create.
                        CE7Town* pNewTown = new CE7Town() ;
                        pNewTown->AddCell(pCell->GetID()) ;
                        FindCellToTown(pCell, pNewTown) ;
                        // TOWN SET CITY.
                        flag = true ;
                    }
                    break ;
                }
            }
            if (!flag)
            {
                break ;
            }
        }
    }

    return true ;
}

void CE7Country::FindCellToTown(CE7Cell* pCell, CE7Town* pTown)
{
    for (int i = 0; i < 6; ++i)
    {
        if (NULL != pCell->GetNeighborPointer(i) &&
            pCell->GetCountryID() == pCell->GetNeighborPointer(i)->GetCountryID() &&
            -1 == pCell->GetNeighborPointer(i)->GetTownID())
        {
            pTown->AddCell(pCell->GetNeighborPointer(i)->GetID()) ;
            FindCellToTown(pCell->GetNeighborPointer(i), pTown) ;
        }
    }
}


















